package indi.defoliation.unknownrpg.api.rpgevent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import indi.defoliation.unknownrpg.api.attribute.Attribute;

public class AttributeActionLogTable {

	private HashMap<Integer,ActionMinute> map = new HashMap<>();

	private int count=0;

	public void addAction(Attribute att,Event d) {
		map.put(count,new ActionMinute(att,d));
		count++;
	}

	@SuppressWarnings("unchecked")
	public Map<Attribute,Event> getAction(){
		return ((Map<Attribute, Event>) map.clone());
	}

	public boolean hasAttribute(Attribute att) {
		for(ActionMinute action : map.values()) {
			if(action.operoter.getKeyName().equals(att.getKeyName()))
				return true;
		}
		return false;
	}

	public int getTimesOfOperotions(Attribute att) {
		count=0;
		for(ActionMinute action : map.values()) {
			if(action.operoter.getKeyName().equals(att.getKeyName()))
				count++;
		}
		return count;
	}

	public Event getFirstAction(Attribute att) {
		for(ActionMinute action : map.values()) 
			if(action.operoter.getKeyName().equals(att.getKeyName()))
				return action.event;
		return null;
	}

	public Event getAction(Attribute att,int times) {
		for(ActionMinute action : map.values()) 
			if(action.operoter.getKeyName().equals(att.getKeyName()))
				if(times!=0)
					times--;
				else
					return action.event;
		return null;
	}
	
	public ActionMinute getActionOfCount(int count) {
		if(count>=this.count)return null;
		return map.get(count);
	}

	public Event[] getActions(Attribute att) {
		ArrayList<Event> list = new ArrayList<>();
		for(ActionMinute action : map.values()) 
			if(action.operoter.getKeyName().equals(att.getKeyName()))
				list.add(action.event);
		return list.toArray(new Event[0]);
	}

	private class ActionMinute{
		public final Attribute operoter;
		public final Event event;
		public ActionMinute(Attribute operoter,Event event) {
			this.operoter=operoter;
			this.event=event;
		}
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		for(Entry<Integer, ActionMinute>  entry: map.entrySet()) {
			sb.append(entry.getKey()+":"+entry.getValue().operoter.getKeyName()+"\n");
		}
		return sb.toString();
	}
}
